﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QuickApp;

namespace QuickApp.DbInterface
{
    public enum DbCommandType : int
    {
        UpdateInsert = 1,
        SelectDataTable = 2,
        SelectScalar = 3
    }
    public abstract class QADbManager : IQAParent
    {

        #region Protected Fields

        protected QADbConnection connection;

        protected List<QADbDatabase> databases;

        protected IDbDataAdapter dbAdapter;

        #endregion Protected Fields

        #region Public Constructors

        public QADbManager()
        {
        }

        #endregion Public Constructors

        #region Public Properties

        public Dictionary<string, List<IQAChild>> Children
        {
            get
            {
                return new Dictionary<string, List<IQAChild>>() { { "Databases", Databases.Cast<IQAChild>().ToList() } };
            }
        }

        public List<QADbDatabase> Databases
        {
            get
            {
                if (databases == null)
                    //TODO: Make into loadChildren() and put method in IParent
                    loadDatabases();
                return databases;
            }
        }

        public bool HasChildren
        {
            get { return Children != null && Children.Count > 0; }
        }

        public string DisplayName
        {
            get { return ""; }
        }

        public string Name
        {
            get { return ""; }
        }

        #endregion Public Properties

        #region Public Methods

        public abstract bool CanConnect();

        public int Delete(string command)
        {
            return Update(command);
        }

        public int Insert(string command)
        {
            return Update(command);
        }

        //TODO: Make NotConnected Exception. Throw it when !CanConnect()
        public object SelectScalar(string command)
        {
            if (CanConnect())
                return getCommand(command, DbCommandType.SelectScalar);
            return null;
        }

        public DataTable SelectTable(string command)
        {
            if (CanConnect())
                return (DataTable)getCommand(command, DbCommandType.SelectDataTable);
            return null;
        }        

        public abstract bool SetConnection(QADbConnection connection);

        public void ChangeCurrentDatabase(string database)
        {
            this.connection.Database = database;
        }

        public int Update(string command)
        {
            if (!String.IsNullOrWhiteSpace(command) && CanConnect())
                return (int)getCommand(command, DbCommandType.UpdateInsert);
            return 0;
        }

        #endregion Public Methods

        #region Protected Methods

        protected abstract object getCommand(string command, DbCommandType cmd);

        protected abstract void loadDatabases();

        #endregion Protected Methods

    }
}
